package com.xxx.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.server.mapper.EmployeeEcMapper;
import com.xxx.server.pojo.Employee;
import com.xxx.server.pojo.EmployeeEc;
import com.xxx.server.service.EmployeeEcService;
import com.xxx.server.service.EmployeeService;
import com.xxx.server.utils.RespPageBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 员工奖罚表 服务实现类
 * </p>
 *
 * @author HeYan
 * @since 2024-05-01
 */
@Service
public class EmployeeEcServiceImpl extends ServiceImpl<EmployeeEcMapper, EmployeeEc> implements EmployeeEcService {

    @Resource
    private EmployeeEcMapper employeeEcMapper;
    @Resource
    private EmployeeService employeeService;

    @Override
    public RespPageBean getEmployeeEcByPage(Integer currentPage, Integer size) {
        //开启分页
        Page<EmployeeEc> page = new Page<>(currentPage, size);
        //开始查询
        IPage<EmployeeEc> employeeByPage = page(page);
        List<EmployeeEc> records = employeeByPage.getRecords();
        List<EmployeeEc> collect = records.stream().map(record -> {
            Employee employee = employeeService.getById(record.getEid());
            record.setEcName(employee.getName());
            if (record.getEcType() == 0) {
                record.setEcRealType("奖励");
            } else {
                record.setEcRealType("惩罚");
            }
            return record;
        }).collect(Collectors.toList());
        RespPageBean respPageBean = new RespPageBean(employeeByPage.getTotal(), collect);
        return respPageBean;
    }

    @Override
    public RespPageBean getEmployeeByPage(Integer currentPage, Integer size, EmployeeEc employee) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getName, employee.getEcName());
        List<Employee> list = employeeService.list(queryWrapper);
        List<Integer> ids = list.stream().map(Employee::getId).collect(Collectors.toList());
        // 开启分页
        Page<EmployeeEc> page = new Page<>(currentPage, size);
        int type  = -1;
        if (employee.getEcRealType() != null) {
            if (employee.getEcRealType().equals("奖励")) {
                type = 0;
            } else {
                type = 1;
            }
        }
        LambdaQueryWrapper<EmployeeEc> eq = new LambdaQueryWrapper<EmployeeEc>().in(ids.size() != 0, EmployeeEc::getEid, ids).eq(type != -1, EmployeeEc::getEcType, type).eq(EmployeeEc::getEcDate, employee.getEcDate());
        Page<EmployeeEc> page1 = page(page, eq);
        List<EmployeeEc> records = page1.getRecords();
        List<EmployeeEc> collect = records.stream().map(record -> {
            Employee employee1 = employeeService.getById(record.getEid());
            record.setEcName(employee1.getName());
            if (record.getEcType() == 0) {
                record.setEcRealType("奖励");
            } else {
                record.setEcRealType("惩罚");
            }
            return record;
        }).collect(Collectors.toList());
        return new RespPageBean(page1.getTotal(), page1.getRecords());
    }
}
